package socketmvc.core.adapter;

import socketmvc.core.codec.IPacketCodec;
import socketmvc.core.codec.ProtocolCodecHandler;
import socketmvc.core.context.IContext;
import socketmvc.core.filter.FilterHandler;
import socketmvc.core.filter.IFilter;
import socketmvc.core.packets.basic.IPacket;
import socketmvc.core.packets.basic.Protocol;
import socketmvc.core.processor.ProcessorMethodHandler;
import socketmvc.core.processor.bean.ProcessorMethod;
import java.nio.ByteBuffer;
import java.util.List;

public class SocketComponentAdapter {


    private final ProcessorMethodHandler processorMethodHandler;
    private final ProtocolCodecHandler protocolCodecHandler;
    private final FilterHandler filterHandler;

    public SocketComponentAdapter(ProcessorMethodHandler processorMethodHandler, ProtocolCodecHandler protocolCodecHandler, FilterHandler filterHandler) {
        this.processorMethodHandler = processorMethodHandler;
        this.protocolCodecHandler = protocolCodecHandler;
        this.filterHandler = filterHandler;
    }

    public IPacketCodec identifyCodec(ByteBuffer buffer){
        return identifyCodec(buffer.get(buffer.position()));
    }

    public IPacketCodec  identifyCodec(IPacket packet){
        return identifyCodec(packet.protocolVersion());
    }

    public IPacketCodec identifyCodec(Protocol protocol){
        return protocolCodecHandler.getProtocolCodec(protocol);
    }

    public IPacketCodec identifyCodec(Byte version){
        return protocolCodecHandler.getProtocolCodec(version);
    }

    public ProcessorMethod identifyProcessorMethod(IPacket packet){
        String cmd = packet.getCommand();
        return processorMethodHandler.getProcessorMethod(cmd);
    }

    public List<IFilter> getFilters(IContext context, IPacket packet){
        return filterHandler.getFilters( context,  packet);
    }

}
